package com.comleader.uap.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.comleader.common.core.web.page.PageResult;
import com.comleader.uap.common.page.PageQuery;
import com.comleader.uap.common.response.Result;
import com.comleader.uap.common.util.ExcelUtil;
import com.comleader.uap.common.util.ResponseUtil;
import com.comleader.uap.mapper.AppAnalyseMapper;
import com.comleader.uap.pojo.entity.AppAnalyse;
import com.comleader.uap.pojo.query.AppAnalyseQuery;
import com.comleader.uap.pojo.vo.AppAnalyseVO;
import com.comleader.uap.pojo.vo.UrlPushVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static cn.hutool.core.date.DateUtil.formatDate;

/**
 * @author 王坤
 * @description 针对表【api_ly_app_analyse(涉诈APP分析表结构)】的数据库操作Service
 * @createDate 2024-11-26 18:10:00
 */
@DS("mysql-ly")
@Service
public class AppAnalyseService extends ServiceImpl<AppAnalyseMapper, AppAnalyse> {

    public List<String> queryLabelType() {
        return baseMapper.queryLabelType();
    }

    public PageResult<AppAnalyseVO> selectList(PageQuery<AppAnalyseQuery> pageQuery) {
        AppAnalyseQuery appAnalyseQuery = pageQuery.getQueryParams();
        if (CollUtil.isNotEmpty(appAnalyseQuery.getDiscoveryTime())) {
            appAnalyseQuery.setDiscoveryStartTimestamp(appAnalyseQuery.getDiscoveryTime().get(0));
            appAnalyseQuery.setDiscoveryEndTimestamp(appAnalyseQuery.getDiscoveryTime().get(1));
        }
        PageHelper.startPage(pageQuery.getPageNumber(), pageQuery.getPageSize());
        List<AppAnalyse> list = baseMapper.selectList(pageQuery.getQueryParams());
        PageInfo pageInfo = new PageInfo<>(list);
        PageResult<AppAnalyseVO> pageResult = new PageResult();
        pageResult.setPageNumber(pageInfo.getPageNum());
        pageResult.setPageSize(pageInfo.getPageSize());
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        List<AppAnalyseVO> voList = new ArrayList<>();
        list.forEach(data -> {
            AppAnalyseVO appAnalyseVO = new AppAnalyseVO();
            appAnalyseVO.setDataId(data.getDataId());
            appAnalyseVO.setAppName(data.getAppName());
            appAnalyseVO.setPackageName(data.getPackageName());
            appAnalyseVO.setLabel(data.getLabel());
            appAnalyseVO.setApkCertificateSha1(data.getApkCertificateSha1());
            appAnalyseVO.setApkCertificateSha256(data.getApkCertificateSha256());
            appAnalyseVO.setApkMd5(data.getApkMd5());
            appAnalyseVO.setCreateAt(data.getCreateAt());
            voList.add(appAnalyseVO);
        });
        pageResult.setRecords(voList);
        return pageResult;
    }

    public void export(AppAnalyseQuery query, HttpServletResponse response) throws IOException {
        List<Date> discoveryTimeList = query.getDiscoveryTime();
        if (CollUtil.isEmpty(query.getDiscoveryTime()) || query.getDiscoveryTime().size() != 2) {
            ResponseUtil.write(response, Result.fail("必须选择开始时间和结束时间"));
            return;
        }
        try {
            query.setDiscoveryStartTimestamp(query.getDiscoveryTime().get(0));
            query.setDiscoveryEndTimestamp(query.getDiscoveryTime().get(1));
            String fileName = "涉诈APP分析_" + formatDate(discoveryTimeList.get(0)) + "_" + formatDate(discoveryTimeList.get(1)) + ".xlsx";
            List<AppAnalyse> list = baseMapper.selectList(query);
            List<AppAnalyseVO> voList = new ArrayList<>();
            list.forEach(data -> {
                AppAnalyseVO appAnalyseVO = new AppAnalyseVO();
                appAnalyseVO.setDataId(data.getDataId());
                appAnalyseVO.setAppName(data.getAppName());
                appAnalyseVO.setPackageName(data.getPackageName());
                appAnalyseVO.setLabel(data.getLabel());
                appAnalyseVO.setApkCertificateSha1(data.getApkCertificateSha1());
                appAnalyseVO.setApkCertificateSha256(data.getApkCertificateSha256());
                appAnalyseVO.setApkMd5(data.getApkMd5());
                appAnalyseVO.setCreateAt(data.getCreateAt());
                voList.add(appAnalyseVO);
            });
            ExcelUtil.exportMultipleSheet(response, fileName, AppAnalyseVO.class, "涉诈APP分析", voList);
        } catch (Exception e) {
            log.error("涉诈APP分析导出异常", e);
            ResponseUtil.write(response, Result.fail("系统异常"));
        }

    }

}
